package jdbctest;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;

	public class StudentDao {
	    
	    public boolean addStudent(student stu) throws SQLException,ClassNotFoundException{
	        Connection conn = ConnectionUtil.getConnection();
	        Statement stmt = conn.createStatement();
	        String sqlString = String.format("insert into df values('%s','%s','%s');", stu.getXm(),stu.getXb(),stu.getTel());

	        return stmt.executeUpdate(sqlString) > 0;
	    }

	    public boolean delStudent(student stu) throws SQLException,ClassNotFoundException{
	        Connection conn = ConnectionUtil.getConnection();
	        Statement stmt = conn.createStatement();
	        String sqlString = "delete from student where xm='"+stu.getXm()+"'";
	        return stmt.executeUpdate(sqlString) > 0;
	    }

	    public boolean updateStudent(student stu) throws SQLException,ClassNotFoundException{
	        Connection conn = ConnectionUtil.getConnection();
	        Statement stmt = conn.createStatement();
	        String sqlString = "update student set xm='"+stu.getXm()+"',xb='"+stu.getXb()+"',tel='"+stu.getTel()+"' where xm='"+stu.getXm()+"'";
	        return  stmt.executeUpdate(sqlString) > 0;
	    }

	    public ArrayList<student> listAllStudent() throws SQLException,ClassNotFoundException{
	        Connection conn = ConnectionUtil.getConnection();
	        Statement stmt = conn.createStatement();
	        ResultSet rs = stmt.executeQuery("select * from hj;");
	        ArrayList<student> list = new ArrayList<student>();
	        while(rs.next()){
	            student student = new student();
	            student.setXm(rs.getString("xm"));
	            student.setXb(rs.getString("xb"));
	            student.setTel(rs.getString("tel"));
	            list.add(student);
	        }
	        return list;
	    }
	}


